package com.abel.bigwater.kmgw.controller

import com.abel.bigwater.kmgw.IwgServer
import com.abel.bigwater.kmgw.KmingServer
import com.abel.bigwater.kmgw.KmingVirtualDeviceBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.ui.set
import org.springframework.web.bind.annotation.RequestMapping
import java.util.*
import javax.servlet.http.HttpServletRequest

@Controller
class HelloController {
    @Autowired
    var kmingServer: KmingServer? = null

    @Autowired
    var kmingGateway: KmingVirtualDeviceBean? = null

    @Autowired
    var iwgServer: IwgServer? = null

    @RequestMapping("/")
    fun index(m: Model): String {
        if (kmingGateway == null || kmingGateway?.cfg == null) {
            return "login"
        }

        m.set("kmingGateway", kmingGateway!!)
        m.set("kmingServer", kmingServer!!)
        m.set("iwgServer", iwgServer!!)

        m.set("curTime", Date())
        return "index"
    }

    @RequestMapping("/login")
    fun login(): String {
        return "login"
    }

    @RequestMapping("/logout")
    fun logout(request: HttpServletRequest): String {
        request.logout()

        return "redirect:/login"
    }
}